package com.haiheng.library_base.http

import com.google.gson.Gson
import com.google.gson.internal.`$Gson$Types`
import okhttp3.ResponseBody

/**
 *
 * @Des:
 * @Author: hwz
 * @Date:2022/4/15 0015
 * @Version: 1.0
 */
class GSonConverter: SimpleHttp.Converter {

    companion object {
        fun create(): GSonConverter {
            return GSonConverter()
        }
    }

    private val gSon = Gson()

    override fun <T> converter(responseBody: ResponseBody, type: Class<T>): T {
        val jsonReader = gSon.newJsonReader(responseBody.charStream())
//        var listType  = `$Gson$Types`.newParameterizedTypeWithOwner(
//            null,
//            ArrayList::class.java, type
//        )
        val adapter = gSon.getAdapter(type)
        return responseBody.use {
            adapter.read(jsonReader)
        }
    }

    override fun <T> fromJson(json: String, classOfT: Class<T>): T {
        return gSon.fromJson(json, classOfT)
    }

}